package zd.cornermemory.fragment;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ReplacementTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import zd.cornermemory.R;
import zd.cornermemory.adapter.ScrambleAdapter;
import zd.cornermemory.bean.CoordinateKeyValue;
import zd.cornermemory.bean.CornerOrEdge;
import zd.cornermemory.bean.Cube;
import zd.cornermemory.bean.CubeColor;
import zd.cornermemory.bean.DisplayBean;
import zd.cornermemory.bean.NumberType;
import zd.cornermemory.min2phase.Search;
import zd.cornermemory.utils.CoordinateUtil;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;
import zd.cornermemory.utils.Statistics;
import zd.cornermemory.view.MyListView;

import static android.content.Context.INPUT_METHOD_SERVICE;
import static java.lang.Integer.parseInt;

/**
 * Created by zhud on 2019/4/29.
 * 生成打乱公式
 */

public class DisruptFragment extends Fragment implements View.OnClickListener{

    private Button   clear_button;
    private Button   disrupt_button;
    private Button   disrupt_button2;
    private TextView result_text;

    private EditText dl_gs;
    private EditText edge_code_length;
    private EditText edge_fanse_count;
    private EditText edge_xunhuan_count;
    private Spinner  edge_is_homing;

    private EditText corner_code_length;
    private EditText corner_fanse_count;
    private EditText corner_xunhuan_count;
    private Spinner  corner_is_homing;

    private Spinner  is_parity;
    private Spinner  is_dl_zb;
    private Spinner  dl_ms;
    private int cornerIshoming = 0;
    private int edgeIshoming = 0;
    private int is_parity_all = 0;
    private int is_dl_zb_all = 0;
    private int dlms_type = 0;

    private LinearLayout code_layout;
    private Button copy_button;

    private TextView corner_buffer;
    private TextView edge_buffer;

    private Toast toast;
    private Toast copyToast;


    private LinearLayout random_type;
    private LinearLayout code_type;
    private EditText corner_code;
    private EditText corner_fanse;
    private EditText edge_code;
    private EditText edge_fanse;

    private MyListView lv;
    private List<String> mList;
    private ScrambleAdapter scrambleAdapter;
    private Context mContext;




    private AlertDialog dialog;
    private AlertDialog.Builder builder;
    private View dialogView;
    private TextView text1;
    private LinearLayout up_corner_0;
    private LinearLayout up_corner_1;
    private LinearLayout up_corner_2;
    private LinearLayout up_corner_3;
    private LinearLayout up_edge_0;
    private LinearLayout up_edge_1;
    private LinearLayout up_edge_2;
    private LinearLayout up_edge_3;
    private LinearLayout up_edge_4;

    private LinearLayout down_corner_0;
    private LinearLayout down_corner_1;
    private LinearLayout down_corner_2;
    private LinearLayout down_corner_3;
    private LinearLayout down_edge_0;
    private LinearLayout down_edge_1;
    private LinearLayout down_edge_2;
    private LinearLayout down_edge_3;
    private LinearLayout down_edge_4;

    private LinearLayout left_corner_0;
    private LinearLayout left_corner_1;
    private LinearLayout left_corner_2;
    private LinearLayout left_corner_3;
    private LinearLayout left_edge_0;
    private LinearLayout left_edge_1;
    private LinearLayout left_edge_2;
    private LinearLayout left_edge_3;
    private LinearLayout left_edge_4;

    private LinearLayout right_corner_0;
    private LinearLayout right_corner_1;
    private LinearLayout right_corner_2;
    private LinearLayout right_corner_3;
    private LinearLayout right_edge_0;
    private LinearLayout right_edge_1;
    private LinearLayout right_edge_2;
    private LinearLayout right_edge_3;
    private LinearLayout right_edge_4;

    private LinearLayout front_corner_0;
    private LinearLayout front_corner_1;
    private LinearLayout front_corner_2;
    private LinearLayout front_corner_3;
    private LinearLayout front_edge_0;
    private LinearLayout front_edge_1;
    private LinearLayout front_edge_2;
    private LinearLayout front_edge_3;
    private LinearLayout front_edge_4;

    private LinearLayout back_corner_0;
    private LinearLayout back_corner_1;
    private LinearLayout back_corner_2;
    private LinearLayout back_corner_3;
    private LinearLayout back_edge_0;
    private LinearLayout back_edge_1;
    private LinearLayout back_edge_2;
    private LinearLayout back_edge_3;
    private LinearLayout back_edge_4;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3, container, false);
        toast = Toast.makeText(getActivity(),getString(R.string.no_exist), Toast.LENGTH_SHORT);
        copyToast = Toast.makeText(getActivity(),getString(R.string.copied), Toast.LENGTH_SHORT);
        mContext = getActivity();
        mList = new ArrayList<>();
        scrambleAdapter = new ScrambleAdapter(mContext, mList);
        initViews(view);
        dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.scramble_dialog,null);
        initDialogViews(dialogView);

        builder = new AlertDialog.Builder(getActivity())

                .setView(dialogView)
                .setCancelable(false)
                .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();


        setEvent();
        setBuffer();
        getActivity().registerReceiver(broadcastReceiver,new IntentFilter(SpKey.MODIFY_BUFFER_SUCCESS));
        return view;
    }

    private void initDialogViews(View view) {

        text1 = view.findViewById(R.id.text1);
        up_corner_0 = view.findViewById(R.id.up_corner_0);
        up_corner_1 = view.findViewById(R.id.up_corner_1);
        up_corner_2 = view.findViewById(R.id.up_corner_2);
        up_corner_3 = view.findViewById(R.id.up_corner_3);
        up_edge_0 = view.findViewById(R.id.up_edge_0);
        up_edge_1 = view.findViewById(R.id.up_edge_1);
        up_edge_2 = view.findViewById(R.id.up_edge_2);
        up_edge_3 = view.findViewById(R.id.up_edge_3);
        up_edge_4 = view.findViewById(R.id.up_edge_4);

        down_corner_0 = view.findViewById(R.id.down_corner_0);
        down_corner_1 = view.findViewById(R.id.down_corner_1);
        down_corner_2 = view.findViewById(R.id.down_corner_2);
        down_corner_3 = view.findViewById(R.id.down_corner_3);
        down_edge_0 = view.findViewById(R.id.down_edge_0);
        down_edge_1 = view.findViewById(R.id.down_edge_1);
        down_edge_2 = view.findViewById(R.id.down_edge_2);
        down_edge_3 = view.findViewById(R.id.down_edge_3);
        down_edge_4 = view.findViewById(R.id.down_edge_4);


        left_corner_0 = view.findViewById(R.id.left_corner_0);
        left_corner_1 = view.findViewById(R.id.left_corner_1);
        left_corner_2 = view.findViewById(R.id.left_corner_2);
        left_corner_3 = view.findViewById(R.id.left_corner_3);
        left_edge_0 = view.findViewById(R.id.left_edge_0);
        left_edge_1 = view.findViewById(R.id.left_edge_1);
        left_edge_2 = view.findViewById(R.id.left_edge_2);
        left_edge_3 = view.findViewById(R.id.left_edge_3);
        left_edge_4 = view.findViewById(R.id.left_edge_4);

        right_corner_0 = view.findViewById(R.id.right_corner_0);
        right_corner_1 = view.findViewById(R.id.right_corner_1);
        right_corner_2 = view.findViewById(R.id.right_corner_2);
        right_corner_3 = view.findViewById(R.id.right_corner_3);
        right_edge_0 = view.findViewById(R.id.right_edge_0);
        right_edge_1 = view.findViewById(R.id.right_edge_1);
        right_edge_2 = view.findViewById(R.id.right_edge_2);
        right_edge_3 = view.findViewById(R.id.right_edge_3);
        right_edge_4 = view.findViewById(R.id.right_edge_4);

        front_corner_0 = view.findViewById(R.id.front_corner_0);
        front_corner_1 = view.findViewById(R.id.front_corner_1);
        front_corner_2 = view.findViewById(R.id.front_corner_2);
        front_corner_3 = view.findViewById(R.id.front_corner_3);
        front_edge_0 = view.findViewById(R.id.front_edge_0);
        front_edge_1 = view.findViewById(R.id.front_edge_1);
        front_edge_2 = view.findViewById(R.id.front_edge_2);
        front_edge_3 = view.findViewById(R.id.front_edge_3);
        front_edge_4 = view.findViewById(R.id.front_edge_4);

        back_corner_0 = view.findViewById(R.id.back_corner_0);
        back_corner_1 = view.findViewById(R.id.back_corner_1);
        back_corner_2 = view.findViewById(R.id.back_corner_2);
        back_corner_3 = view.findViewById(R.id.back_corner_3);
        back_edge_0 = view.findViewById(R.id.back_edge_0);
        back_edge_1 = view.findViewById(R.id.back_edge_1);
        back_edge_2 = view.findViewById(R.id.back_edge_2);
        back_edge_3 = view.findViewById(R.id.back_edge_3);
        back_edge_4 = view.findViewById(R.id.back_edge_4);
    }

    private TextWatcher textWatcher1 = new TextWatcher() {
        String key = SpKey.CORNER_CODE_LENGTH;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,corner_code_length.getText().toString());
        }
    };
    private TextWatcher textWatcher2 = new TextWatcher() {
        String key = SpKey.CORNER_FANSE_COUNT;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,corner_fanse_count.getText().toString());
        }
    };
    private TextWatcher textWatcher3 = new TextWatcher() {
        String key = SpKey.CORNER_XUNHUAN_COUNT;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,corner_xunhuan_count.getText().toString());
        }
    };
    private TextWatcher textWatcher4 = new TextWatcher() {
        String key = SpKey.EDGE_CODE_LENGTH;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,edge_code_length.getText().toString());
        }
    };
    private TextWatcher textWatcher5 = new TextWatcher() {
        String key = SpKey.EDGE_FANSE_COUNT;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,edge_fanse_count.getText().toString());
        }
    };
    private TextWatcher textWatcher6 = new TextWatcher() {
        String key = SpKey.EDGE_XUNHUAN_COUNT;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,edge_xunhuan_count.getText().toString());
        }
    };
    private TextWatcher textWatcher7 = new TextWatcher() {
        String key = SpKey.CORNER_CODE;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,corner_code.getText().toString());
        }
    };
    private TextWatcher textWatcher8 = new TextWatcher() {
        String key = SpKey.CORNER_FANSE;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,corner_fanse.getText().toString());
        }
    };
    private TextWatcher textWatcher9 = new TextWatcher() {
        String key = SpKey.EDGE_CODE;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,edge_code.getText().toString());
        }
    };
    private TextWatcher textWatcher10 = new TextWatcher() {
        String key = SpKey.EDGE_FANSE;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,edge_fanse.getText().toString());
        }
    };

    private TextWatcher textWatcher11 = new TextWatcher() {
        String key = SpKey.DL_GS;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            String content = s == null ? null : s.toString();
            if(s == null || s.length() == 0) {
                saveEditStr(key,"");
                return;
            }
            if (content.startsWith("+") || content.startsWith("-")) {
                s.delete(0,1);
                return;
            }
            int size = content.length();
            if(content.substring(0, size - 1).contains("+")) {
                s.delete(size-1, size);
                return;
            }
            if(content.endsWith("+")||content.endsWith("-")){
                String str = content.substring(0, size - 1);
                if (str.contains("+") || str.contains("-")) {
                    s.delete(size-1, size);
                }
            }
            saveEditStr(key,dl_gs.getText().toString());
        }
    };

    private void saveEditStr(String key,String value) {
        SPUtils.getInstance().put(key,value);
    }

    private void setEvent() {

        corner_code.setTransformationMethod(new UpperCaseTransform());
        corner_fanse.setTransformationMethod(new UpperCaseTransform());
        edge_code.setTransformationMethod(new UpperCaseTransform());
        edge_fanse.setTransformationMethod(new UpperCaseTransform());
        corner_code.addTextChangedListener(textWatcher7);
        corner_fanse.addTextChangedListener(textWatcher8);
        edge_code.addTextChangedListener(textWatcher9);
        edge_fanse.addTextChangedListener(textWatcher10);
        dl_gs.addTextChangedListener(textWatcher11);


        corner_code_length.addTextChangedListener(textWatcher1);
        corner_fanse_count.addTextChangedListener(textWatcher2);
        corner_xunhuan_count.addTextChangedListener(textWatcher3);
        edge_code_length.addTextChangedListener(textWatcher4);
        edge_fanse_count.addTextChangedListener(textWatcher5);
        edge_xunhuan_count.addTextChangedListener(textWatcher6);

        disrupt_button.setOnClickListener(this);
        if (disrupt_button2 != null) {
            disrupt_button2.setOnClickListener(this);
        }
        clear_button.setOnClickListener(this);
        copy_button.setOnClickListener(this);

        String cornerCodeLength = SPUtils.getInstance().getString(SpKey.CORNER_CODE_LENGTH,"");
        corner_code_length.setText(cornerCodeLength);
        corner_fanse_count.setText(SPUtils.getInstance().getString(SpKey.CORNER_FANSE_COUNT,""));
        corner_xunhuan_count.setText(SPUtils.getInstance().getString(SpKey.CORNER_XUNHUAN_COUNT,""));
        edge_code_length.setText(SPUtils.getInstance().getString(SpKey.EDGE_CODE_LENGTH,""));
        edge_fanse_count.setText(SPUtils.getInstance().getString(SpKey.EDGE_FANSE_COUNT,""));
        edge_xunhuan_count.setText(SPUtils.getInstance().getString(SpKey.EDGE_XUNHUAN_COUNT,""));

        corner_code.setText(SPUtils.getInstance().getString(SpKey.CORNER_CODE,""));
        corner_fanse.setText(SPUtils.getInstance().getString(SpKey.CORNER_FANSE,""));
        edge_code.setText(SPUtils.getInstance().getString(SpKey.EDGE_CODE,""));
        edge_fanse.setText(SPUtils.getInstance().getString(SpKey.EDGE_FANSE,""));
        dl_gs.setText(SPUtils.getInstance().getString(SpKey.DL_GS,"1"));



        String[] isHome = getActivity().getResources().getStringArray(R.array.is_homing);
        List<String> list = new ArrayList<>();
        for(int i=0;i<isHome.length;i++) {
            list.add(isHome[i]);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),

                R.layout.spinner_checked_text, list);

        corner_is_homing.setAdapter(adapter);
        int cornerIndex = SPUtils.getInstance().getInt(SpKey.CORNER_IS_HOMING, 0);
        corner_is_homing.setSelection(cornerIndex);
        corner_is_homing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                hideSoftInput();
                cornerIshoming = i;
                SPUtils.getInstance().put(SpKey.CORNER_IS_HOMING, i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                hideSoftInput();
            }
        });
        edge_is_homing.setAdapter(adapter);
        int edgeIndex = SPUtils.getInstance().getInt(SpKey.EDGE_IS_HOMING, 0);
        edge_is_homing.setSelection(edgeIndex);
        edge_is_homing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                hideSoftInput();
                edgeIshoming = i;
                SPUtils.getInstance().put(SpKey.EDGE_IS_HOMING, i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                hideSoftInput();
            }
        });

        String[] isParity = getActivity().getResources().getStringArray(R.array.is_parity);
        List<String> listParity = new ArrayList<>();
        for(int i=0;i<isParity.length;i++) {
            listParity.add(isParity[i]);
        }
        ArrayAdapter<String> adapterParity = new ArrayAdapter<String>(getContext(),

                R.layout.spinner_checked_text, listParity);
        is_parity.setAdapter(adapterParity);
        int index = SPUtils.getInstance().getInt(SpKey.IS_PARITY);
        is_parity.setSelection(index);
        is_parity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                hideSoftInput();
                is_parity_all = i;
                SPUtils.getInstance().put(SpKey.IS_PARITY,i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                hideSoftInput();
            }
        });

        String[] isDlzb = getActivity().getResources().getStringArray(R.array.is_zuobiao);
        List<String> listDlzb = new ArrayList<>();
        for(int i=0;i<isDlzb.length;i++) {
            listDlzb.add(isDlzb[i]);
        }
        ArrayAdapter<String> adapterDlzb = new ArrayAdapter<String>(getContext(),

                R.layout.spinner_checked_text, listDlzb);
        is_dl_zb.setAdapter(adapterDlzb);

        int position = 0;
        String isDlzbStr = SPUtils.getInstance().getString(SpKey.IS_DL_ZB);
        if(TextUtils.isEmpty(isDlzbStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_DL_ZB,"0");
        }else {
            position = parseInt(isDlzbStr);
        }
        is_dl_zb.setSelection(position);
        is_dl_zb.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                hideSoftInput();
                is_dl_zb_all = i;
                SPUtils.getInstance().put(SpKey.IS_DL_ZB,is_dl_zb_all+"");
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                hideSoftInput();
            }
        });

        String[] dsms = getActivity().getResources().getStringArray(R.array.dlms);
        List<String> listDlms = new ArrayList<>();
        for(int i=0;i<dsms.length;i++) {
            listDlms.add(dsms[i]);
        }
        ArrayAdapter<String> adapterDlms = new ArrayAdapter<String>(getContext(),

                R.layout.spinner_checked_text, listDlms);
        dl_ms.setAdapter(adapterDlms);
        int dlmsIndex = SPUtils.getInstance().getInt(SpKey.DL_MS,0);
        dl_ms.setSelection(dlmsIndex);
        dl_ms.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                hideSoftInput();
                dlms_type = i;
                SPUtils.getInstance().put(SpKey.DL_MS,i);
                switch (dlms_type) {
                    case 0:
                        random_type.setVisibility(View.VISIBLE);
                        code_type.setVisibility(View.GONE);
                        corner_code_length.setEnabled(true);
                        corner_fanse_count.setEnabled(true);
                        corner_xunhuan_count.setEnabled(true);
                        corner_is_homing.setEnabled(true);

                        edge_code_length.setEnabled(true);
                        edge_fanse_count.setEnabled(true);
                        edge_xunhuan_count.setEnabled(true);
                        edge_is_homing.setEnabled(true);

                        is_parity.setEnabled(true);
                        break;
                    case 1:
                        random_type.setVisibility(View.GONE);
                        code_type.setVisibility(View.VISIBLE);
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                hideSoftInput();
            }
        });

    }

    private void clearTj() {
        hideSoftInput();
        code_layout.setVisibility(View.GONE);
//        result_text.setText("");
        mList.clear();
        scrambleAdapter.notifyDataSetChanged();
        edge_code_length.setText("");
        edge_fanse_count.setText("");
        edge_xunhuan_count.setText("");
        corner_code_length.setText("");
        corner_fanse_count.setText("");
        corner_xunhuan_count.setText("");

        corner_code.setText("");
        corner_fanse.setText("");
        edge_code.setText("");
        edge_fanse.setText("");

        edge_is_homing.setSelection(0);
        corner_is_homing.setSelection(0);
        is_parity.setSelection(0);
        is_dl_zb.setSelection(0);
    }

    private void initViews(View view) {
        clear_button = view.findViewById(R.id.clear_button);
        disrupt_button = view.findViewById(R.id.disrupt_button);
        disrupt_button2 = view.findViewById(R.id.disrupt_button2);
//        result_text = view.findViewById(R.id.result_text);
        edge_code_length = view.findViewById(R.id.edge_code_length);
        edge_fanse_count = view.findViewById(R.id.edge_fanse_count);
        edge_xunhuan_count = view.findViewById(R.id.edge_xunhuan_count);
        edge_is_homing = view.findViewById(R.id.edge_is_homing);
        corner_code_length = view.findViewById(R.id.corner_code_length);
        corner_fanse_count = view.findViewById(R.id.corner_fanse_count);
        corner_xunhuan_count = view.findViewById(R.id.corner_xunhuan_count);
        corner_is_homing = view.findViewById(R.id.corner_is_homing);
        is_parity = view.findViewById(R.id.is_parity);
        is_dl_zb = view.findViewById(R.id.is_dl_zb);
        code_layout = view.findViewById(R.id.code_layout);
        copy_button = view.findViewById(R.id.copy_button);
        corner_buffer = view.findViewById(R.id.corner_buffer);
        edge_buffer = view.findViewById(R.id.edge_buffer);
        dl_ms = view.findViewById(R.id.dl_ms);
        random_type = view.findViewById(R.id.random_type);
        code_type = view.findViewById(R.id.code_type);
        corner_code = view.findViewById(R.id.corner_code);
        corner_fanse = view.findViewById(R.id.corner_fanse);
        edge_code = view.findViewById(R.id.edge_code);
        edge_fanse = view.findViewById(R.id.edge_fanse);
        dl_gs = view.findViewById(R.id.dl_gs);
        lv = view.findViewById(R.id.lv);
        lv.setAdapter(scrambleAdapter);
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                copyScramble();
                return true;
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String sc = mList.get(i);
                text1.setText((i+1)+". "+sc);
                if (mList.size() == 1) {
                    text1.setText(sc);
                }
                Cube showCube = new Cube();
                showCube.init();
                showCube.disrupt(Statistics.getFormatCodes(sc));
                List<DisplayBean> displayBeanList =  showCube.getCubeColorStatusData();
                show(displayBeanList);

                dialog.show();

            }
        });
//        result_text.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View view) {
//                //获取剪贴板管理器：
//                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
//                // 创建普通字符型ClipData
//                ClipData mClipData = ClipData.newPlainText("Label", result_text.getText().toString());
//                // 将ClipData内容放到系统剪贴板里。
//                cm.setPrimaryClip(mClipData);
//                copyToast.show();
//                return true;
//            }
//        });
    }


    private void show(List<DisplayBean> displayBeanList){
        for (DisplayBean displayBean:displayBeanList) {
            switch (displayBean.getCubeDirection()){
                case UP:
                    up_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    up_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    up_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    up_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    up_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    up_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    up_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    up_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    up_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));

                    break;
                case DOWN:
                    down_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    down_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    down_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    down_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    down_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    down_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    down_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    down_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    down_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));

                    break;
                case LEFT:
                    left_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    left_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    left_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    left_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    left_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    left_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    left_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    left_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    left_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));

                    break;
                case RIGHT:
                    right_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    right_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    right_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    right_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    right_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    right_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    right_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    right_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    right_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));

                    break;
                case FRONT:
                    front_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    front_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    front_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    front_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    front_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    front_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    front_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    front_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    front_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));

                    break;
                case BACK:
                    back_corner_0.setBackground(getDisplayColor(displayBean.getCornerColors().get(0)));
                    back_corner_1.setBackground(getDisplayColor(displayBean.getCornerColors().get(1)));
                    back_corner_2.setBackground(getDisplayColor(displayBean.getCornerColors().get(2)));
                    back_corner_3.setBackground(getDisplayColor(displayBean.getCornerColors().get(3)));

                    back_edge_0.setBackground(getDisplayColor(displayBean.getEdgeColors().get(0)));
                    back_edge_1.setBackground(getDisplayColor(displayBean.getEdgeColors().get(1)));
                    back_edge_2.setBackground(getDisplayColor(displayBean.getEdgeColors().get(2)));
                    back_edge_3.setBackground(getDisplayColor(displayBean.getEdgeColors().get(3)));
                    back_edge_4.setBackground(getDisplayColor(displayBean.getEdgeColors().get(4)));
                    break;
            }
        }
    }

    private Drawable getDisplayColor(CubeColor colorEnum){
        Drawable color = null;
        switch (colorEnum) {
            case YELLOW:
                color = getResources().getDrawable(R.drawable.color_yellow_bg);
                break;
            case WIHTE:
                color = getResources().getDrawable(R.drawable.color_white_bg);
                break;
            case BLUE:
                color = getResources().getDrawable(R.drawable.color_blue_bg);
                break;
            case GREEN:
                color = getResources().getDrawable(R.drawable.color_green_bg);
                break;
            case RED:
                color = getResources().getDrawable(R.drawable.color_red_bg);
                break;
            case ORANGE:
                color = getResources().getDrawable(R.drawable.color_orange_bg);
                break;
        }

        return color;
    }

    private NumberType getNumberType(String code) {
        String between = "(\\d{1,5})(\\-)(\\d{1,5})";
        String plus = "(\\d{1,5})(\\+)";
        String minus = "(\\d{1,5})(\\-)";
        String number = "\\d{1,5}";
        boolean isBetween = code.matches(between);
        boolean isPlus = code.matches(plus);
        boolean isMinus = code.matches(minus);
        boolean isNumber = code.matches(number);
        if (isBetween) {
            return NumberType.BETWEEN;
        } else if (isPlus) {
            return NumberType.PLUS;
        }else if (isMinus) {
            return NumberType.MINUS;
        }else if (isNumber) {
            return NumberType.NUMBER;
        }else {
            return NumberType.NONE;
        }
    }

    /**
     *
     * @param cornerOrEdge 角或棱
     * @return 二维数组 分别表示 编码长度、翻色数量、小循环数量的取值范围
     */
    private int[][] getRanges(CornerOrEdge cornerOrEdge){
        int[] codeLengthRange = getRange(cornerOrEdge, 0);
        int[] fanseLengthRange = getRange(cornerOrEdge, 1);
        int[] xunhuanLengthRange = getRange(cornerOrEdge, 2);

        int[][] range = {codeLengthRange,fanseLengthRange,xunhuanLengthRange};
        return range;
    }

    /**
     *
     * @param cornerOrEdge 角或棱
     * @param position 0 编码长度 1 翻色数量 2 小循环数
     * @return
     */
    private int[] getRange(CornerOrEdge cornerOrEdge,int position){
        int[] range = {};
        switch (cornerOrEdge) {
            case CORNER:
                if (position == 0) {
                    String cornerCodeLength = corner_code_length.getText().toString().trim();
                    range = getRangeType(cornerCodeLength);
                } else if (position == 1) {
                    String cornerFansecount = corner_fanse_count.getText().toString().trim();
                    range = getRangeType(cornerFansecount);
                }else {
                    String cornerXunhuanCount = corner_xunhuan_count.getText().toString().trim();
                    range = getRangeType(cornerXunhuanCount);
                }
                break;
            case EDGE:
                if (position == 0) {
                    String edgeCodeLength = edge_code_length.getText().toString().trim();
                    range = getRangeType(edgeCodeLength);
                } else if (position == 1) {
                    String edgeFansecount = edge_fanse_count.getText().toString().trim();
                    range = getRangeType(edgeFansecount);
                }else {
                    String edgeXunhuanCount = edge_xunhuan_count.getText().toString().trim();
                    range = getRangeType(edgeXunhuanCount);
                }
                break;
        }

        return range;
    }


    private int[] getRangeType(String counts){
        NumberType numberType = getNumberType(counts);
        int[] rangeType = {};
        switch (numberType) {
            case BETWEEN:
                String[] s = counts.split("-");
                int start = Integer.parseInt(s[0]);
                int end = Integer.parseInt(s[1]);
                rangeType = new int[]{start,end};
                break;
            case PLUS:
                String[] plus = counts.split("\\+");
                int startPlus = Integer.parseInt(plus[0]);
                rangeType = new int[]{startPlus,20};
                break;
            case MINUS:
                String[] minus = counts.split("\\-");
                int endMinus = Integer.parseInt(minus[0]);
                rangeType = new int[]{0,endMinus};
                break;
            case NUMBER:
                int count = parseInt(counts);
                rangeType = new int[]{count,count};
                break;
            case NONE:
                rangeType = new int[]{0,20};//不填写则给个最大范围
                break;
        }
        return rangeType;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.disrupt_button2:
            case R.id.disrupt_button:
                hideSoftInput();

                if (dlms_type == 0) {
                    int gs = 1;//生成打乱公式个数，默认1个
                    try {
                        String g = dl_gs.getText().toString();
                        gs = Integer.parseInt(g);
                    } catch (Exception e) {
                        e.printStackTrace();
                        gs = 1;
                    }
                    List<Cube> cubes = new ArrayList<>();
                    for (int i = 0;i<gs;i++){
                        Cube cube = new Cube();
                        cube.init();

                        int[][] cornerRanges = getRanges(CornerOrEdge.CORNER);
                        int[][] edgeRanges = getRanges(CornerOrEdge.EDGE);
                        boolean isExist = cube.cubeRandomStatus(cornerRanges,edgeRanges,cornerIshoming,edgeIshoming,is_parity_all);
                        if (isExist) {
                            cubes.add(cube);
                        }
                    }
                    if (cubes.size() == 0) {
                        toast.show();
                        break;
                    }
                    toScramble2(cubes);
                }else if(dlms_type==1){
                    randomCodeType();
                }

                break;
            case R.id.clear_button:
                clearTj();
                break;
            case R.id.copy_button:
                copyScramble();

                break;

        }
    }

    private void copyScramble() {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        String copyStr = "";
        for (int i = 0;i<mList.size();i++) {
            String result = mList.get(i);
            copyStr+=((i+1)+". "+result);
            if(i<mList.size()-1){
                copyStr+="\n\n";
            }
        }
        if (mList.size() == 1) {//防止生成单条打乱的时候，复制进读码里，显示前面的序号，导致无法读码
            copyStr = mList.get(0);
        }
        ClipData mClipData = ClipData.newPlainText("Label", copyStr);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        copyToast.show();
    }

    public class UpperCaseTransform extends ReplacementTransformationMethod {
        @Override
        protected char[] getOriginal() {
            char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
            return aa;
        }

        @Override
        protected char[] getReplacement() {
            char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
            return cc;
        }
    }

    private void toScramble(final Cube cube) {
        code_layout.setVisibility(View.VISIBLE);
//        result_text.setText(getString(R.string.generate_));
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = scramble(cube);
                if (TextUtils.isEmpty(result)) {
                    mHandler.sendEmptyMessage(11);
                    return;
                }
                Message message = Message.obtain();
                mList.clear();
                mList.add(result);
                message.what = 22;
                message.obj = result;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    private void toScramble2(final List<Cube> cubes) {
        code_layout.setVisibility(View.VISIBLE);
//        result_text.setText(getString(R.string.generate_));
        new Thread(new Runnable() {
            @Override
            public void run() {
                String res = "";
                int index = 0;
                mList.clear();
                for (int i = 0;i<cubes.size();i++) {
                    Cube cube = cubes.get(i);
                    String result = scramble(cube);
                    mList.add(result);
                    if (!TextUtils.isEmpty(result)) {
                        if (cubes.size() == 1) {
                            res += result;
                            break;
                        }
                        index++;
                        res+=(index+". "+result);
                        if(i<cubes.size()-1){
                            res+="\n\n";
                        }
                    }

                }

                Message message = Message.obtain();
                message.what = 22;
                message.obj = res;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    private void randomCodeType(){
        String cornerCode = corner_code.getText().toString();
        String cornerFanse = corner_fanse.getText().toString();
        String edgeCode = edge_code.getText().toString();
        String edgeFanse = edge_fanse.getText().toString();
        cornerCode = cornerCode.replace(" ", "");
        cornerFanse = cornerFanse.replace(" ", "");
        edgeCode = edgeCode.replace(" ", "");
        edgeFanse = edgeFanse.replace(" ", "");
        if (cornerCode.length() % 2 != edgeCode.length() % 2) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            toast.show();
            return;
        }
        if (cornerFanse.length() > 14) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            toast.show();
            return;
        }
        if (edgeFanse.length() > 22) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            toast.show();
            return;
        }

        if (cornerFanse.length() % 2 != 0 || edgeFanse.length() % 2 != 0) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            toast.show();
            return;
        }

        if (cornerCode.length() == 0 && cornerFanse.length() == 0 && edgeCode.length() == 0 && edgeFanse.length() == 0) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            return;
        }

        Cube cube = new Cube();
        cube.init();
        String result = cube.codeType(cornerCode.toUpperCase(), cornerFanse.toUpperCase(), edgeCode.toUpperCase(), edgeFanse.toUpperCase());
        if (result != null) {
//            result_text.setText("");
            code_layout.setVisibility(View.GONE);
            toast.show();
            return;
        }

        if (cornerFanse.length() >= 10 && edgeFanse.length() > 10) {
            cube.setIs23(true);
        }else {
            cube.setIs23(false);
        }
        toScramble(cube);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 11:
                    code_layout.setVisibility(View.GONE);
                    toast.show();
                    break;
                case 22:
                    String disrupt = (String) msg.obj;
//                    result_text.setText(Statistics.getFormatCodes(disrupt));
//                    result_text.setText(disrupt);
                    scrambleAdapter.notifyDataSetChanged();
                    break;
                case 33:
                    String result = (String) msg.obj;
//                    result_text.setText(Statistics.getFormatCodes(result));
//                    result_text.setText(result);
                    scrambleAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };


    private String scramble(Cube cube) {
        if (cube == null) {
            return null;
        }
        String phaseStr = cube.get2phaseStr();
        int step = 21;
        if (cube.is23()) {
            step = 23;
        }
        String result = new Search().solution(phaseStr, step, 100000000, 0, Search.INVERSE_SOLUTION);

        if (result.contains("Error")) {
            return null;
        }

        if (is_dl_zb_all == 0) {//打乱坐标任意
            Random random = new Random();
            int r = random.nextInt(24);
            if (r != 1) {
                return dlzb(cube, step);
            }else {
                return Statistics.getFormatCodes(result);
            }
        } else if (is_dl_zb_all == 1) {//打乱坐标
            return dlzb(cube, step);
        }else {//不打乱坐标
            return result;
        }

    }

    private String dlzb(Cube cube, int step) {
        CoordinateKeyValue keyValue = CoordinateUtil.coordinateKeyValues.get(new Random().nextInt(CoordinateUtil.coordinateKeyValues.size()));
        cube.disrupt(keyValue.getDisrupt());
        String phaseStr2 = cube.get2phaseStr();
        String result2 = new Search().solution(phaseStr2, step, 100000000, 0, Search.INVERSE_SOLUTION);
        String disrupt = result2+keyValue.getDisplay();

        return Statistics.getFormatCodes(disrupt);
    }

    private void setBuffer() {
        String cornerBufferStr = SPUtils.getInstance().getString(SpKey.CORNER_BUFFER);
        String[] corner = getResources().getStringArray(R.array.corner_buffer);
        if(TextUtils.isEmpty(cornerBufferStr)){
            corner_buffer.setText(getString(R.string.corner_buffer)+corner[1]);
        }else{
            corner_buffer.setText(getString(R.string.corner_buffer)+corner[parseInt(cornerBufferStr)]);
        }

        String edgeBufferStr = SPUtils.getInstance().getString(SpKey.EDGE_BUFFER);
        String[] edge = getResources().getStringArray(R.array.edge_buffer);

        if (TextUtils.isEmpty(edgeBufferStr)) {
            edge_buffer.setText(getString(R.string.edge_buffer)+edge[2]);
        }else {
            edge_buffer.setText(getString(R.string.edge_buffer)+edge[parseInt(edgeBufferStr)]);
        }
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case SpKey.MODIFY_BUFFER_SUCCESS:
                    setBuffer();
                    break;
            }
        }
    };
    private void hideSoftInput() {
        try{
            InputMethodManager mInputMethodManager = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
            assert mInputMethodManager != null;
            mInputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

        }catch (Exception e){

        }
    }








}
